<?php
    // +----------------------------------------------------------------------
    // | 海豚PHP框架 [ DolphinPHP ]
    // +----------------------------------------------------------------------
    // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
    // +----------------------------------------------------------------------
    // | 官方网站: http://dolphinphp.com
    // +----------------------------------------------------------------------
    // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------

    namespace app\member\api;


    use app\common\controller\API;
    use app\member\model\MessageModel;

    class Message extends API
    {

        /**
         * 消息列表
         * api post /member/message/lists
         *
         * @param int $list_rows 每页显示多少条数据
         * @param int $page      当前页数
         */
        public function lists()
        {

            $data = $this->request->param();
            $this->check_validate($data, 'Message.lists');
            $list_rows = $data['list_rows'];
            $order_list = MessageModel::lists(env('member_id'), $list_rows);

            $this->result($order_list);
        }

        /**
         * 已读消息
         * api post /member/message/read
         *
         * @param int $id 消息的ID
         */
        public function read()
        {

            $data = $this->request->param();

            $this->check_validate($data, 'Message.read');

            $res = MessageModel::update(['is_read' => 1], ['member_id' => env('member_id'), 'id' => $data['id']]);
            $res ? $this->success('已经阅读此消息') : $this->error('系统错误！');
        }

        /**
         * 查看消息
         * api post /member/message/read
         *
         * @param int $id 消息的ID
         *
         * @throws \think\exception\DbException
         */
        public function view()
        {

            $data = $this->request->param();

            $this->check_validate($data, 'Message.read');

            $res = MessageModel::get($data['id']);
            $res ? $this->success('已经阅读此消息') : $this->error('系统错误！');
        }

        /**
         * 未阅读消息数量
         * api post /member/message/not_read_number
         *
         * @param int $id 消息的ID
         */

        public function not_read_number()
        {

            $res = MessageModel::where(['is_read' => 0, 'member_id' => env('member_id')])->count();
            $this->result($res);
        }

    }